#include "serializedshape.h"

SerializedShape::SerializedShape(QAbstractGraphicsShapeItem *shape)
{
    type = shape->type();

    switch(type)
    {
        default:
            x = 0;
            y = 0;
            break;

        case(QGraphicsRectItem::Type):
        {
            QGraphicsRectItem *rectangleShape = dynamic_cast<QGraphicsRectItem *>(shape);
            x = rectangleShape->rect().x();
            y = rectangleShape->rect().y();
            break;
        }

        case(QGraphicsEllipseItem::Type):
        {
            QGraphicsEllipseItem *circleShape = dynamic_cast<QGraphicsEllipseItem *>(shape);
            x = circleShape->rect().x();
            y = circleShape->rect().y();
            break;
        }

        case(QGraphicsPolygonItem::Type):
        {
            QGraphicsPolygonItem *triangleShape = dynamic_cast<QGraphicsPolygonItem *>(shape);
            x = triangleShape->boundingRect().x();
            y = triangleShape->boundingRect().y();
            break;
        }
    }

    QColor color = shape->brush().color();
    red = color.red();
    green = color.green();
    blue = color.blue();
}

SerializedShape::SerializedShape(QString serializedShape)
{
    QStringList elementList = serializedShape.split(" ");

    x = elementList[0].toInt();
    y = elementList[1].toInt();
    type = elementList[2].toInt();
    red = elementList[3].toInt();
    green = elementList[4].toInt();
    blue = elementList[5].toInt();
}

std::string SerializedShape::toString()
{
    StringBuilder s;

    s.add(x);
    s.add(" ");
    s.add(y);
    s.add(" ");
    s.add(type);
    s.add(" ");
    s.add(red);
    s.add(" ");
    s.add(green);
    s.add(" ");
    s.add(blue);
    s.add("\n");

    return s.toString();
}

int SerializedShape::getX() { return x; }
int SerializedShape::getY() { return y; }
int SerializedShape::getType() { return type; }
int SerializedShape::getRed() { return red; }
int SerializedShape::getGreen() { return green; }
int SerializedShape::getBlue() { return blue; }
